package org.mybatis3.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;


import java.io.IOException;
import java.io.InputStream;

public class MyBatisUtil {

    private static SqlSession sqlSession;

    private MyBatisUtil(){

    }

    public static SqlSession getSqlSession(){
        if(sqlSession==null){
            try {
                InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
                sqlSession=new SqlSessionFactoryBuilder().build(is).openSession(false);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return sqlSession;
    }

    public static void closeSqlSession(){
        if (sqlSession!=null){
            sqlSession.close();
        }

    }

}
